package com.hongchen.wechat.controller;

import com.hongchen.model.ActUser;
import com.hongchen.model.WechatHouses;
import com.hongchen.wechat.config.Api;
import com.hongchen.wechat.service.HouseService;
import com.jfinal.weixin.sdk.api.ApiResult;


import java.util.Date;

import static com.jfinal.aop.Enhancer.enhance;

/**
 * Created by 3426624897@qq.com on 2018/3/6 0006.
 */
public class HousesController extends Api {

    private final HouseService houseService=enhance(HouseService.class);
    ActUser actUser=new ActUser();
    WechatHouses wechatHouses=new WechatHouses();
    static String openId;
    //用于储存用户数据
    static ApiResult apiResult;

    public void index(){
        apiResult= userdetail();
        openId = apiResult.get("openid");
        if(openId == null){
            this.redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId()+"&redirect_uri=http://"+chengDong()+".tunnel.echomod.cn/houses&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect");
        }else{
            user();
        }
    }
    public void success(){
        render("houses_success.html");
    }

    public void user(){
        ActUser a= houseService.openid(apiResult.getStr("openid"));
        //若用户为第一次进入储存数据 并且可以获取到openid
        if(a==null && apiResult.getStr("openid")!=null){
            //储存openid
            actUser.set("wechat_openid",apiResult.getStr("openid"));
            //储存用户名称
            actUser.set("wechat_nickname",apiResult.getStr("nickname"));
            //性别
            actUser.set("sex",apiResult.getInt("sex"));
            //城市
            actUser.set("city",apiResult.getStr("city"));
            //头像
            actUser.set("head_img",apiResult.getStr("headimgurl"));
            //时间
            actUser.set("creatr_time",new Date());

            actUser.save();
        }else{
            //老用户更新时间
            a.setUpdateTime(new Date());
            a.update();
        }
        //传入当前id
        setAttr("id",houseService.openid(apiResult.getStr("openid")).getId());
        render("houses_index.html");
    }


    /**
     * 用户提交
     */
    public void submit(){
        String id=getPara("id");
        //姓名
        String name=getPara("name");
        //手机
        String tel=getPara("tel");
        //目的
        String purchase=getPara("purchase");
        //区域
        String area=getPara("intention.area");
        //楼盘
        String building=getPara("intention.building");
        ActUser au =houseService.wechat_id(id);
        if(name!=null){
            au.set("name",name);
        }
        if(tel!=null){
            au.set("tel",tel);
            au.update();
        }
        if(purchase!=null){
            wechatHouses.set("purpose",purchase);
        }
        if(area!=null){
            wechatHouses.set("intention",area);
        }
        if(building!=null){
            wechatHouses.set("houses",building);
        }
        wechatHouses.set("wechat_user_id",au.getId());
        if(wechatHouses.save()){
            renderJson(true);
        }else{
            renderJson(false);
        }


    }
}
